var DictComm = {
    Url: CTX + '/sys/dict'
};
var DictMVC = {
    Req: {
        list: {method: 'GET', url: DictComm.Url},
        add: {method: 'POST', url: DictComm.Url},
        update: {method: 'PUT', url: DictComm.Url},
        del: {method: 'DELETE', url: DictComm.Url},
        export: {method: 'GET', url: DictComm.Url + '/export'}
    },
    DOMs: null,
    Columns: {
        dict: [[{
            field: 'id',
            title: '字典ID',
            width: 80,
            hidden: true
        }, {
            field: 'type',
            title: '字典类型',
            width: 120,
            sortable: true,
            formatter: function (v, row, i) {
                return '<a href="javascript:;" class="row-btn" title="' + row.type + '" onclick="see(' + i + ')">' + v + '</a>';
            }
        }, {
            field: 'text',
            title: '展示文本',
            width: 100
        }, {
            field: 'value',
            title: '字典值',
            width: 80
        }, {
            field: 'sort',
            title: '顺序',
            width: 80
        }, {
            field: 'cls',
            title: '样式',
            width: 150
        }, {
            field: 'note',
            title: '备注',
            width: 150
        }, {
            field: '_opts_',
            title: '操作',
            width: 60,
            align: 'center',
            formatter: function (v, row, i) {
                var ary = [];
                ary.push('<a href="javascript:;" permission="sys:dict:update" class="row-btn" title="修改" onclick="update(' + i + ')"><i class=" fa fa-edit"></i></a>');
                ary.push('<a href="javascript:;" permission="sys:dict:del" class="row-btn" title="删除" onclick="deletes(' + i + ')"><i class="fa fa-trash"></i></a>');
                return ary.join('&nbsp;<span class="btn-separator"></span>&nbsp;');
            }
        }]]
    }
};

$(function () {
    DictMVC.DOMs = {
        $searchForm: $('#searchForm'),
        $searchBtn: $('#searchBtn'),
        $resetBtn: $('#resetBtn'),
        $dataGrid: $('#dictGrid'),
        $contextMenu: $('#cm'),
        $dlg: $('#dlg')
    };

    initPage(DictMVC.DOMs, DictMVC.Req, {
        singleSelect: true,
        sortName: 'type,sort',
        sortOrder: 'asc,asc',
        columns: DictMVC.Columns.dict,
        onLoadSuccess: function (data) {
            $.app.controlPermission();
            if (data && data.rows && data.rows.length > 0) {

            }
        }
    }, getQueryParams, true);
});

// 数据查询方法，作用于查询、导出
function getQueryParams() {
    return DictMVC.DOMs.$searchForm.serializeForm();
}

// 添加
function add(defaultRow) {
    var $dlg = DictMVC.DOMs.$dlg, $grid = DictMVC.DOMs.$dataGrid;
    var $form = $dlg.find('form');
    $form.form('reset');

    if (defaultRow) {
        $form.form('load', defaultRow);
    }

    $dlg.dialog({
        title: '新增字典',
        modal: true,
        inline: true,
        buttons: [{
            text: '确定',
            iconCls: 'e-icon fa fa-save',
            plain: true,
            handler: function () {
                var formObj = $form.serializeForm();
                $form.submitEForm(DictMVC.Req.add, formObj, function (id) {
                    $grid.datagrid('reload', {
                        id: id
                    });
                    $dlg.dialog('close');
                });
            }
        }, {
            text: '关闭',
            iconCls: 'fa fa-close',
            plain: true,
            handler: function () {
                $dlg.dialog('close');
            }
        }]
    }).dialog('open');
}

// 添加同类型字典
function addSameTypeDict() {
    var row = DictMVC.DOMs.$dataGrid.datagrid('getSelections')[0];
    add({type: row.type});
}

// 修改
function update(rowIndex) {
    var $dlg = DictMVC.DOMs.$dlg, $grid = DictMVC.DOMs.$dataGrid;
    var row = null;
    if (rowIndex || rowIndex === 0) {// 第0行也可以修改
        row = $grid.datagrid("getRows")[rowIndex];
    } else {
        row = $grid.datagrid('getSelections')[0];
    }
    if (!row) {
        $.showMsg('请选择一行数据！');
        return;
    }

    var $form = $dlg.find('form');
    $form.form('load', row);
    $dlg.dialog({
        title: '修改字典',
        modal: true,
        buttons: [{
            text: '确定',
            iconCls: 'fa fa-save',
            plain: true,
            handler: function () {
                var formObj = $form.serializeForm();
                $form.submitEForm(DictMVC.Req.update, formObj, function (id) {
                    $grid.datagrid('reload', {
                        id: id
                    });
                    $dlg.dialog('close');
                });
            }
        }, {
            text: '关闭',
            iconCls: 'fa fa-close',
            plain: true,
            handler: function () {
                $dlg.dialog('close');
            }
        }]
    }).dialog('open');
}

// 查看
function see(rowIndex) {
    var $dlg = DictMVC.DOMs.$dlg, $grid = DictMVC.DOMs.$dataGrid;
    var row = $grid.datagrid("getRows")[rowIndex];

    $dlg.find('form').form('load', row);
    $dlg.dialog({
        title: '查看字典',
        modal: true,
        buttons: [{
            text: '关闭',
            iconCls: 'fa fa-close',
            plain: true,
            handler: function () {
                $dlg.dialog('close');
            }
        }]
    }).dialog('open');
}

// 删除，包括行内删除和批量删除
function deletes(rowIndex) {
    var $grid = DictMVC.DOMs.$dataGrid;
    if (rowIndex || rowIndex === 0) {// 第0行也可以删除
        var row = $grid.datagrid("getRows")[rowIndex];
        $.confirmMsg('确定要删除字典[' + row.type + '][' + row.text + ']吗？', function () {
            doDelete([row.id], $grid);
        });
    } else {
        var rows = $grid.datagrid('getSelections'), ids = [];
        if (rows && rows.length) {
            $.each(rows, function (i, row) {
                ids.push(row.id);
            });
            $.confirmMsg('确定要删除选择的[' + ids.length + ']条记录？', function () {
                doDelete(ids, $grid);
            });
        } else {
            $.showMsg('请选择要删除的记录');
        }
    }
}

// 请求后台删除
function doDelete(ids, $reloadGid) {
    $.ajax({
        type: DictMVC.Req.del.method,
        url: DictMVC.Req.del.url,
        data: {
            ids: ids.join(',')
        },
        dataType: 'json',
        success: function (result) {
            $reloadGid.datagrid('reload');
        }
    });
}